import { Context } from 'koa'
import { AuthService } from '@/services/auth'

export default class AuthController {
    static async login(ctx: Context) {
        try {
            const { nickName, avatar, code } = ctx.request.body
            console.log('ctx.request.body', ctx.request.body)
            const result = await AuthService.login({ code, nickName, avatar })
            ctx.state.isLogin = true
            ctx.body = {
                code: 0,
                data: result,
                message: '登录成功'
            }
        } catch (error) {
            ctx.status = 400
            ctx.body = {
                code: 400,
                message: error.message
            }
        }
    }

    static async query(ctx: Context) {
        try {
            const { username } = ctx.request.body
            const result = await AuthService.getUserInfo(username)

            ctx.body = {
                code: 0,
                data: result,
                message: '查询成功'
            }
        } catch (error) {
            ctx.status = 400
            ctx.body = {
                code: 400,
                message: error.message
            }
        }
    }
}
